// app.js
const { skipDomainCheck, env } = require("./config/api.js");
const { checkAuthBeforeNavigate } = require("./utils/auth-helper.js");

App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync("logs") || [];
    logs.unshift(Date.now());
    wx.setStorageSync("logs", logs);

    // 开发环境下跳过域名校验
    if (skipDomainCheck && env === "development") {
      console.log("开发环境：跳过域名校验");
      wx.setEnableDebug({
        enableDebug: true,
      });
    }

    // 初始化用户状态管理
    const userStore = require("./store/userStore.js");
    userStore.init();
  },

  // 更新自定义TabBar状态
  updateCustomTabBar() {
    // 获取当前页面
    const pages = getCurrentPages();
    if (pages.length > 0) {
      const currentPage = pages[pages.length - 1];
      const customTabBar = currentPage.getTabBar();
      if (customTabBar) {
        // 更新选中状态
        customTabBar.updateSelectedFromCurrentPage();
        // 更新购物车数量
        customTabBar.getCartCount();
      }
    }
  },

  onShow() {
    console.log("App Show");
  },

  onHide() {
    console.log("App Hide");
  },

  onError(msg) {
    console.log("App Error", msg);
  },

  globalData: {
    userInfo: null,
    appId: "wx14d0d8f57cefc6d9",
    secret: "b75f0ca6b5c7fcc5136bdb8bfd6b2bbd",
    // mchId: "1602204428",
    jumpHistory: [],
    cartCount: 0,
    addCarAction: false, // 记录用户是否执行了加入购物车操作
    currentTabIndex: 0, // 当前选中的Tab索引
  },
});
